package view;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import controller.JobApplicationController;
import controller.SessionController;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;
import model.*;
import utils.JSFUtils;
import com.itextpdf.text.Font.*;
import java.io.FileOutputStream;
import utils.PDFServlet;

/**
 *
 * @author ZimmE
 */
@Named
@ConversationScoped
public class JobApplicationView implements Serializable {

    @EJB
    private JobApplicationController jobApplicationController;
    @EJB
    private SessionController sessionController;
    @Inject
    private Conversation conversation;
    private Availability availability;
    private Person person;
    private PersonCompetence personCompetence;
    private Role role;
    private User user;
    private UserRole userRole;
    private List<PersonCompetence> personCompetenceList;
    private List<Competence> competenceList;

    /**
     * Creates a new instance of JobApplicationView
     */
    public JobApplicationView() {
        availability = new Availability();
        person = new Person();
        user = new User();
        user.setPersonId(person);
        personCompetence = new PersonCompetence();
        role = new Role();
        userRole = new UserRole();
        availability.setPersonId(person);
        personCompetence.setPersonId(person);
        user.setPersonId(person);
        userRole.setRoleId(role);
        userRole.setUserId(user);
        personCompetenceList = new ArrayList<PersonCompetence>();
        competenceList = null;
    }

    /**
     * Get the value of userRole
     *
     * @return the value of userRole
     */
    public UserRole getUserRole() {
        return userRole;
    }

    /**
     * Set the value of userRole
     *
     * @param userRole new value of userRole
     */
    public void setUserRole(UserRole userRole) {
        this.userRole = userRole;
    }

    /**
     * Get the value of user
     *
     * @return the value of user
     */
    public User getUser() {
        return user;
    }

    /**
     * Set the value of user
     *
     * @param user new value of user
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * Get the value of role
     *
     * @return the value of role
     */
    public Role getRole() {
        return role;
    }

    /**
     * Set the value of role
     *
     * @param role new value of role
     */
    public void setRole(Role role) {
        this.role = role;
    }

    /**
     * Get the value of personCompetence
     *
     * @return the value of personCompetence
     */
    public PersonCompetence getPersonCompetence() {
        return personCompetence;
    }

    /**
     * Set the value of personCompetence
     *
     * @param personCompetence new value of personCompetence
     */
    public void setPersonCompetence(PersonCompetence personCompetence) {
        this.personCompetence = personCompetence;
    }

    /**
     * Get the value of person
     *
     * @return the value of person
     */
    public Person getPerson() {
        if (sessionController.isAuthenticated()) {
            user = jobApplicationController.getCurrentUser();
            person = user.getPersonId();
        }
        return person;
    }

    /**
     * Set the value of person
     *
     * @param person new value of person
     */
    public void setPerson(Person person) {
        this.person = person;
    }

    /**
     * Get the value of availability
     *
     * @return the value of availability
     */
    public Availability getAvailability() {
        return availability;
    }

    /**
     * Set the value of availability
     *
     * @param availability new value of availability
     */
    public void setAvailability(Availability availability) {
        this.availability = availability;
    }

    private void start() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

    private void end() {
        if (!conversation.isTransient()) {
            conversation.end();
        }
    }

    public String processPersonalInfo() {
        start();
        return "Competence" + "?faces-redirect=true";
    }

    public String processCompetence() {
        return "Availability" + "?faces-redirect=true";
    }

    public String procesAvailability() {
        return "Summary" + "?faces-redirect=true";
    }

    public void processJobApplication() {
        JSFUtils.addMessage(FacesMessage.SEVERITY_INFO, "Job application saved!");
        end();
    }

    /**
     * Method is used to print out a PDF version of the job application
     * @throws IOException
     * @throws DocumentException
     */
    public void printPDF() throws IOException, DocumentException {
        Document doc = new Document(PageSize.A4);
        com.itextpdf.text.Font font = FontFactory.getFont("Helvetica", 18);
        try {
            //Path needs to be changed...
            PdfWriter.getInstance(doc, new FileOutputStream("C:/test.pdf"));
            doc.open();
            Paragraph text = new Paragraph("Job application", font);
            doc.add(text);
            text = new Paragraph("First name:" + getPerson().getFirstName());
            doc.add(text);
            text = new Paragraph("Last name:" + getPerson().getLastName());
            doc.add(text);
            text = new Paragraph("From date: " + getAvailability().getFromDate().toString());
            doc.add(text);
            text = new Paragraph("To date: " + getAvailability().getToDate().toString());
            doc.add(text);
            text = new Paragraph("Competence: " + getPersonCompetence().toString());
            doc.add(text);
            doc.close();
        } catch (DocumentException ex) {
            Logger.getLogger(JobApplicationView.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public List<Competence> getCompetenceList() {
        if (competenceList == null) {
            competenceList = jobApplicationController.getCompetenceList();
        }
        return competenceList;
    }

    public void addCompetence() {
        personCompetenceList.add(personCompetence);
        competenceList.remove(personCompetence.getCompetenceId());
        personCompetence = new PersonCompetence();
        personCompetence.setCompetenceId(new Competence());
    }

    public void delCompetence() {
        PersonCompetence pc = personCompetenceList.get(personCompetenceList.size() - 1);
        personCompetenceList.remove(pc);
        competenceList.add(pc.getCompetenceId());

    }

    public List<PersonCompetence> getPersonCompetenceList() {
        return personCompetenceList;
    }
}
